package com.ebt.m.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebt.m.R;


/**
 * 副标题
 */
public class SubTitleView extends LinearLayout {
    private String mTitleString = "标题"; // TODO: use a default from R.string...
    private int mTitleColor = Color.parseColor("#8f8f8f"); // TODO: use a default from R.color...
    private float mTitleDimension = 14; // TODO: use a default from R.dimen...
    private Drawable mTitleDrawable;

    TextView txtTitle;
    ImageView imageView;

    public SubTitleView(Context context) {
        super(context);
    }


    public SubTitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.sub_title_view_c, this, true);
        final TypedArray a = context.obtainStyledAttributes(
                attrs, R.styleable.SubTitleView);

        mTitleString = a.getString(
                R.styleable.SubTitleView_txtTitleString);
        mTitleColor = a.getColor(
                R.styleable.SubTitleView_txtTitleColor,
                mTitleColor);
        // Use getDimensionPixelSize or getDimensionPixelOffset when dealing with
        // values that should fall on pixel boundaries.
        mTitleDimension = a.getDimension(
                R.styleable.SubTitleView_txtTitleDimension,
                mTitleDimension);

        if (a.hasValue(R.styleable.SubTitleView_titleDrawable)) {
            mTitleDrawable = a.getDrawable(
                    R.styleable.SubTitleView_titleDrawable);
            mTitleDrawable.setCallback(this);
        }
        a.recycle();


    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        txtTitle = (TextView) findViewById(R.id.txt_subtitle);
        imageView = (ImageView) findViewById(R.id.img_subtile);

        setmTitleString(mTitleString);
        setmTitleColor(mTitleColor);
        setmTitleDimension(mTitleDimension);
        setmTitleDrawable(mTitleDrawable);
    }

    public String getmTitleString() {
        return mTitleString;
    }

    public void setmTitleString(String mTitleString) {
        this.mTitleString = mTitleString;
        txtTitle.setText(mTitleString);
    }

    public int getmTitleColor() {
        return mTitleColor;
    }

    public void setmTitleColor(int mTitleColor) {
        this.mTitleColor = mTitleColor;
        txtTitle.setTextColor(mTitleColor);
    }

    public float getmTitleDimension() {
        return mTitleDimension;
    }

    public void setmTitleDimension(float mTitleDimension) {
        this.mTitleDimension = mTitleDimension;
        txtTitle.setTextSize(mTitleDimension);
    }

    public Drawable getmTitleDrawable() {
        return mTitleDrawable;
    }

    public void setmTitleDrawable(Drawable mTitleDrawable) {
        this.mTitleDrawable = mTitleDrawable;
        if (mTitleDrawable != null) {
            imageView.setImageDrawable(mTitleDrawable);
        }
    }
}
